﻿using TI_BLE_HCI_ClientLib.DataEntities.Advertising;
using TI_BLE_HCI_ClientLib.GenericHCIEntities;
using TI_BLE_HCI_ClientLib.HCIBaseEntities;

namespace TI_BLE_HCI_ClientLib.HCIEvents
{
    public class HCIEvent_GAP_Device_Information : HCIVendorSpecificEvent
    {
        public enum EventTypeEnum
        {
            ConnectableUndirectedAdvertisement = 0,
            ConnectableDirectedAdvertisement = 1,
            DiscoverableUndirectedAdvertisement = 2,
            NonConnectableUndirectedAdvertisement = 3,
            ScanResponse = 4
        }

        public enum AddrTypeEnum
        {
            ADDRTYPE_PUBLIC = 0,
            ADDRTYPE_STATIC = 1,
            ADDRTYPE_PRIVATE_NONRESOLVE = 2,
            ADDRTYPE_PRIVATE_RESOLVE = 3
        }

        private HCIVendorSpecificEventHostErrorCode _status;
        private EventTypeEnum _eventType;
        private AddrTypeEnum _addrType;
        private byte[] _addr;
        private sbyte _rssi;
        private byte[] _dataField;

        private AdvertisingAndScanResponseData _advertisingAndScanResponseData;

        public AdvertisingAndScanResponseData AdvertisingAndScanResponseData
        {
            get { return _advertisingAndScanResponseData; }
        }

        public HCIVendorSpecificEventHostErrorCode Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public EventTypeEnum EventType
        {
            get { return _eventType; }
            set { _eventType = value; }
        }

        public AddrTypeEnum AddrType
        {
            get { return _addrType; }
            set { _addrType = value; }
        }

        public byte[] Addr
        {
            get { return _addr; }
            set { _addr = value; }
        }

        public sbyte Rssi
        {
            get { return _rssi; }
            set { _rssi = value; }
        }

        public byte[] DataField
        {
            get { return _dataField; }
            set { _dataField = value; updateAdvertisingAndScanResponseData(); }
        }

        public HCIEvent_GAP_Device_Information(
            HCIVendorSpecificEventHostErrorCode status,
            EventTypeEnum eventType,
            AddrTypeEnum addrType,
            byte[] addr,
            sbyte rssi,
            byte[] dataField)
            : base((ushort)HCIVendorSpecificEventEventOpcode.GAP_Device_Information)
        {
            _status = status;
            _eventType = eventType;
            _addrType = addrType;
            _addr = addr;
            _rssi = rssi;
            _dataField = dataField;

            updateAdvertisingAndScanResponseData();
        }

        private void updateAdvertisingAndScanResponseData()
        {
            _advertisingAndScanResponseData = AdvertisingAndScanResponseDataParser.Parse(_dataField);
        }
    }
}